import os

import logging
from logging.handlers import RotatingFileHandler

import tornado.web
import tornado.ioloop
import tornado.httpserver

from controller import *


def make_app():
    application = tornado.web.Application([

        # 系统管理
        (r"/_dev/system/list", system_api.ListHandler),
        (r"/_dev/system/add", system_api.AddHandler),
        (r"/_dev/system/edit", system_api.EditHandler),
        (r"/_dev/system/del", system_api.DelHandler),
        (r"/_dev/system/select", system_api.SelectHandler),
        (r"/_dev/system/tree", system_api.TreeHandler),
        (r"/_dev/system/export", system_api.ExportHandler),
        (r"/_dev/system/import", system_api.ImportHandler),

        # 模块管理
        (r"/_dev/module/list", module_api.ListHandler),
        (r"/_dev/module/add", module_api.AddHandler),
        (r"/_dev/module/edit", module_api.EditHandler),
        (r"/_dev/module/del", module_api.DelHandler),
        (r"/_dev/module/select", module_api.SelectHandler),

        # 资源管理
        (r"/_dev/resource/list", resource_api.ListHandler),
        (r"/_dev/resource/add", resource_api.AddHandler),
        (r"/_dev/resource/edit", resource_api.EditHandler),
        (r"/_dev/resource/del", resource_api.DelHandler),
        (r"/_dev/resource/select", resource_api.SelectHandler),
        (r"/_dev/database/select", database_api.SelectHandler),
        (r"/_dev/database/db/list", database_api.DbListHandler),
        (r"/_dev/database/table/list", database_api.TableListHandler),
        (r"/_dev/database/field/list", database_api.FieldListHandler),

        # 接口管理
        (r"/_dev/interface/list", interface_api.ListHandler),
        (r"/_dev/interface/add", interface_api.AddHandler),
        (r"/_dev/interface/batch_add", interface_api.BatchAddHandler),
        (r"/_dev/interface/edit", interface_api.EditHandler),
        (r"/_dev/interface/del", interface_api.DelHandler),
        (r"/_dev/interface/parser_param", interface_api.ParamListHandler),
        (r"/_dev/interface/format_sql", interface_api.FormatHandler),
        (r"/_dev/interface/create_template", interface_api.CreateTemplateHandler),

        # 参数校验
        (r"/_dev/verify/list", verify_api.ListHandler),
        (r"/_dev/verify/add", verify_api.AddHandler),
        (r"/_dev/verify/edit", verify_api.EditHandler),
        (r"/_dev/verify/del", verify_api.DelHandler),

        # 静态资源
        (r"/_dev/(.*)", tornado.web.StaticFileHandler, {"path": "static","default_filename":"index.html"},),
    ]
    )

    return application


def run():
    if not os.path.exists("../logs"):
        os.mkdir("../logs")
    fmt = logging.Formatter("%(asctime)s|%(levelname)s|%(process)s|%(filename)s|%(funcName)s|%(lineno)s|%(message)s")

    sh = logging.StreamHandler()
    sh.setFormatter(fmt)
    sh.setLevel(logging.INFO)

    fh = RotatingFileHandler(filename="../logs/crud_dev.log", maxBytes=10 * 1024 * 1024, backupCount=10)
    fh.setFormatter(fmt)
    fh.setLevel(logging.INFO)
    logging.basicConfig(level=logging.INFO, handlers=[sh, fh])

    listen_address = "0.0.0.0"
    listen_port = 8890

    logging.info("开始启动,监听地址[%s:%d]" % (listen_address, listen_port))

    app = make_app()
    http_server = tornado.httpserver.HTTPServer(app)
    http_server.listen(port=listen_port, address=listen_address)
    tornado.ioloop.IOLoop.current().start()


if __name__ == "__main__":
    run()
